let ParserFactory = require('../Parsers/ParserFactory');
let Puller = require('../Pullers/BasePuller');
const fs = require('fs');

class MainController {
    constructor(type) {
        this.puller = new Puller();
        this.BASE_URL = 'http://www.zujuan.com'
    }
    start() {
        let that = this;
        this.puller
            .getPull(that.BASE_URL)
            .then(res => {
                let catalogsParser = new ParserFactory('catalogsParser');
                let data = catalogsParser.parser(res);
                // console.log(data);
                that.deepPull(data);                
            })
            .catch(err => {
                throw err;
            })
    }
    deepPull(data) {
        let that = this;
        (async function() {
            function sleep(time) {
                return new Promise((resolve, reject) => {
                    setTimeout(resolve, time);
                })
            }
            let index = 0
            for (let item of data) {
                let subItems = item.subject_items;
                for (const subItem of subItems) {
                    console.log(subItem);
                    let fileName = subItem.title + subItem.qst_text;
                    let reqUrl = that.BASE_URL + subItem.qst_url
                    console.log(`request_url : ${reqUrl}.............................`);
                    that.puller
                        .getPull(reqUrl)
                        .then(data => {
                            // console.log(data);
                            that.deepParser(data, fileName);
                        })
                    await sleep(10000)
                }
            }
        })(data)
    }
    deepParser(data, fileName) {
        let that = this;
        let questionsParser = new ParserFactory('questionsParser');
        let result = questionsParser.parser(data);
        that.writeFile('./data/'+fileName+'.json',JSON.stringify(result));
    }
    writeFile(url,data) {
        fs.writeFile(url, data, err => {
            if (err) {
                throw err;
            }else {
                console.log(`${url}: 写入成功!`)
            }
        })
    }
}

module.exports = MainController;